﻿using System;
using System.Collections.Generic;
using net.xBei.Models.Request;

namespace net.xBei.Models.Response {
    /// <summary>
    /// 搜索API返回
    /// </summary>
    /// <typeparam name="TRequest"></typeparam>
    /// <typeparam name="TResult"></typeparam>
    public class SearchResponse<TRequest, TResult> where TRequest : SearchRequest {
        /// <summary>
        /// /
        /// </summary>
        public SearchResponse() {
        }
        /// <summary>
        /// 空队列
        /// </summary>
        /// <param name="request"></param>
        public SearchResponse(TRequest request) : this(request, 0, default) { }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="request"></param>
        /// <param name="total"></param>
        /// <param name="items"></param>
        public SearchResponse(TRequest request, int total, IEnumerable<TResult>? items) {
            Total = total;
            Page = request.Page ?? 1;
            PageSize = request.GetLimit();
            Items = items;
            Key = request.Key;
            RequestId = request.RequestId ?? Guid.NewGuid().ToString();
        }
        /// <summary>
        /// 总数量
        /// </summary>
        public int Total { get; set; }
        /// <summary>
        /// 分页
        /// </summary>
        public int Page { get; set; }
        /// <summary>
        /// 分页大小
        /// </summary>
        public int PageSize { get; set; }
        /// <summary>
        /// 分页数量
        /// </summary>
        public int PageCount {
            get {
                return (int)Math.Ceiling((decimal)Total / PageSize);
            }
        }
        /// <summary>
        /// 数据
        /// </summary>
        public virtual IEnumerable<TResult>? Items { get; set; }
        /// <summary>
        /// 搜索关键字
        /// </summary>
        public string? Key { get; set; }
        /// <summary>
        /// 客户端请求ID，服务器会直接原样返回
        /// </summary>
        public string? RequestId { get; set; }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public static SearchResponse<TRequest, TResult> Empty(TRequest request) {
            return new SearchResponse<TRequest, TResult>(request, 0, Array.Empty<TResult>());
        }
    }
    /// <summary>
    /// 
    /// </summary>
    /// <typeparam name="TRequest"></typeparam>
    public class SearchResponse<TRequest> where TRequest : SearchRequest {
        /// <summary>
        /// 总数量
        /// </summary>
        public int Total { get; set; }
        /// <summary>
        /// 分页
        /// </summary>
        public int Page { get; set; }
        /// <summary>
        /// 分页大小
        /// </summary>
        public int PageSize { get; set; }
        /// <summary>
        /// 分页数量
        /// </summary>
        public int PageCount {
            get {
                return (int)Math.Ceiling((decimal)Total / PageSize);
            }
        }
        /// <summary>
        /// 数据
        /// </summary>
        public IEnumerable<object> Items { get; set; }
        /// <summary>
        /// 搜索关键字
        /// </summary>
        public string? Key { get; set; }
        /// <summary>
        /// 客户端请求ID，服务器会直接原样返回
        /// </summary>
        public string? RequestID { get; set; }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="request"></param>
        /// <param name="total"></param>
        /// <param name="items"></param>
        public SearchResponse(TRequest request, int total, IEnumerable<object> items) {
            Total = total;
            Page = request.Page ?? 1;
            PageSize = request.GetLimit();
            Items = items;
            Key = request.Key;
            RequestID = request.RequestId ?? Guid.NewGuid().ToString();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public static SearchResponse<TRequest> Empty(TRequest request) {
            return new SearchResponse<TRequest>(request, 0, Array.Empty<object>());
        }
    }
    /// <summary>
    /// 
    /// </summary>
    public class SearchResponse {
        /// <summary>
        /// 总数量
        /// </summary>
        public int Total { get; set; }
        /// <summary>
        /// 分页
        /// </summary>
        public int Page { get; set; }
        /// <summary>
        /// 分页大小
        /// </summary>
        public int PageSize { get; set; }
        /// <summary>
        /// 分页数量
        /// </summary>
        public int PageCount {
            get {
                return (int)Math.Ceiling((decimal)Total / PageSize);
            }
        }
        /// <summary>
        /// 数据
        /// </summary>
        public IEnumerable<object> Items { get; set; }
        /// <summary>
        /// 搜索关键字
        /// </summary>
        public string Key { get; set; }
        /// <summary>
        /// 客户端请求ID，服务器会直接原样返回
        /// </summary>
        public string RequestID { get; set; }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="key"></param>
        /// <param name="page"></param>
        /// <param name="pageSize"></param>
        /// <param name="total"></param>
        /// <param name="items"></param>
        /// <param name="requestId"></param>
        public SearchResponse(string key, int page, int pageSize, int total, IEnumerable<object> items, string? requestId) {
            Total = total;
            Page = page;
            PageSize = pageSize;
            Items = items;
            Key = key;
            RequestID = requestId ?? Guid.NewGuid().ToString();
        }
    }
}
